/***************************************************************
 * Name:      SetupCreatorMain.h
 * Purpose:   Defines Application Frame
 * Author:    Jeremie Fouche (jeremie.fouche@gmail.com)
 * Created:   2007-05-05
 * Copyright: Jeremie Fouche ()
 * License:
 **************************************************************/

#ifndef SC_SETUPFRAME_H
#define SC_SETUPFRAME_H

#include <wx/validate.h>

#include "setupapp.h"
#include "setupdlg.h"
#include "setupgui.h"

#include "sc/context.h"

#include <map>

/**
* @class SetupFrame
*/
class SetupFrame : public SetupFrameGui
{
public:
    SetupFrame(wxWindow* parent);

protected:
    /**
    * @class ElementTID
    *
    * represent the tree item data
    */
    class ElementTID : public wxTreeItemData
    {
    public:
        ElementTID(sc::SetupContext* pSetupCtx, sc::setup::Element* pElement)
        : m_pSetupCtx(pSetupCtx)
        , m_pElement(pElement)
        {
            assert(pSetupCtx && pElement);
        }

    public:
        sc::SetupContext*   m_pSetupCtx;
        sc::setup::Element* m_pElement;
    };

	typedef std::map<std::string,int> TIcons;

    /// GUI ID
    enum GuiID {
        ID_MENU_ADD_BEGIN    = 20000,
        ID_MENU_ADD_END      = 20099
    };

    /// Attributes
    sc::Context   m_context;
	TIcons        m_mapIcons;

    /**
    * @class AttributeValidator
    */
	class AttributeValidator : public wxValidator
	{
    private:
        const sc::cfg::Attribute* m_pcAttribute;

    public:
	    AttributeValidator(const sc::cfg::Attribute& rcAttribute)
	    : m_pcAttribute(&rcAttribute) { }

	    AttributeValidator(const AttributeValidator& val)
	    : wxValidator()
	    {
	        Copy(val);
	    }

        virtual wxObject *Clone() const
        {
            return new AttributeValidator(*this);
        }

        bool Copy(const AttributeValidator& val)
        {
            wxValidator::Copy(val);
            m_pcAttribute = val.m_pcAttribute;
            return true;
        }

	    virtual bool Validate(wxWindow* parent)
	    {
	        bool bValid = m_pcAttribute->Validate( m_validatorWindow->GetLabel().c_str() );
	        if (!bValid)
                ::wxMessageBox(_T("Valeur incorrecte"));
            return bValid;
	    }
	};

public:
    int LoadContext(const wxString& sFilename);
    void SetContextReadOnly(bool bReadOnly);

protected:
    /// Setup tree methods
    void Load(void);
    wxTreeItemId AddTreeChild(const wxTreeItemId& hParent, const sc::SetupContext* pcSetupCtx, const sc::setup::Element* pcElement);
    sc::SetupContext* GetSetupCtx(const wxTreeItemId& tid) const;
    sc::setup::Element* GetElement(const wxTreeItemId& tid) const;
    wxString GetErrorSC(int error) const;

    /// Attributes panel methods
    void RemoveAllControls(void);
	void SetControls(sc::SetupContext& rSetupCtx, sc::setup::Element& rElement);
    void SaveAttributes(sc::setup::Element& rElement);

    /// Context methods
	int AddChild(sc::SetupContext& rSetupCtx, sc::setup::Element& rParent, const std::string& sChild, const wxTreeItemId& hParent);
	int DeleteElement(sc::SetupContext& rSetupCtx, sc::setup::Element* pElement, const wxTreeItemId& hElement);

    /// Locals event handler
    void OnContextMenuAddChild( wxCommandEvent& event );

    /// Virtual event handlers, from wxFB SCFrameGui class
    virtual void OnFileOpen( wxCommandEvent& event );
    virtual void OnFileSave( wxCommandEvent& event );
    virtual void OnFileSaveAs( wxCommandEvent& event );
    virtual void OnUpdateFileSaveAs( wxUpdateUIEvent& event );
    virtual void OnFileExit( wxCommandEvent& event );
    virtual void OnEditAddChild( wxCommandEvent& event );
    virtual void OnUpdateEditAddChild( wxUpdateUIEvent& event );
    virtual void OnEditDeleteChild( wxCommandEvent& event );
    virtual void OnUpdateEditDeleteChild( wxUpdateUIEvent& event );
    virtual void OnTreeSetupContextMenu( wxTreeEvent& event );
    virtual void OnHelpAbout( wxCommandEvent& event );
    virtual void OnTreeSetupSelChanged( wxTreeEvent& event );
    virtual void OnTreeSetupSelChanging( wxTreeEvent& event );
    virtual void OnTreeSetupRightClick( wxTreeEvent& event );

private:

    DECLARE_EVENT_TABLE()
};


/**
* @class DlgAbout
*/
class DlgAbout : public DlgAboutGui
{
public:
    DlgAbout(wxWindow* parent)
    : DlgAboutGui(parent)
    {
        m_txtVersion->SetLabel(SC_VERSION);
    }
};

#endif // SC_SETUPFRAME_H
